import { SimulateController } from "./Controller";
import { InfoLevel } from "./_";

export default class Ue4Service {

  controller: SimulateController;

  constructor(c: SimulateController) {
    this.controller = c;
  }

  private sendSignal(index: number) {
    for (const [i, v] of this.controller.toolbarService.signalList.entries()) {
      if (i === index) {
        this.controller.toolbarService.signalEmit(String(v.id));
        break;
      }
    }
  }

  async transfer(message: Ue4TransferMessage) {
    switch (message.data.actionType) {
      case Ue4TransferActionType.load:
        await this.controller.toolbarService.initSimulation();
        break;
      case Ue4TransferActionType.start:
        await this.controller.toolbarService.startSimulation();
        break;
      case Ue4TransferActionType.continue:
        await this.controller.toolbarService.continueSimulation();
        break;
      case Ue4TransferActionType.pause:
        await this.controller.toolbarService.pauseSimulation();
        break;
      case Ue4TransferActionType.finish:
        await this.controller.toolbarService.finishSimulation();
        break;
      case Ue4TransferActionType.setStepInterval:
        await this.controller.toolbarService.handleSpeedChange(message.data.actionData.stepValue);
        break;
      case Ue4TransferActionType.stepOne:
        await this.controller.toolbarService.stepOne();
        break;
      case Ue4TransferActionType.sendSignal:
        this.sendSignal(message.data.actionData.signalIndex);
        break;
      case Ue4TransferActionType.log:
        this.controller.consoleService.addRecord({ message: message.data.actionData.logdata, level: InfoLevel.INFO });
        break;
      default:
    }
  }

}

const enum Ue4TransferActionType {
  load = "load",
  start = "start",
  continue = "continue",
  pause = "pause",
  finish = "finish",
  setStepInterval = "setStepInterval",
  stepOne = "stepOne",
  sendSignal = "sendSignal",
  log = "log",
}

export type Ue4TransferMessage = {
  api:string;
  data:{
    actionType: Ue4TransferActionType,
    actionData?: any,
  }
}
